package principal;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author jose cardenas
 */
public class JIFNoPago extends javax.swing.JInternalFrame {

    /**
     * Creates new form JIFNoPago
     */
    public JIFNoPago() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTDatos = new javax.swing.JTable();
        jCBAnio = new javax.swing.JComboBox();
        jCBMes = new javax.swing.JComboBox();
        jBcancelar = new javax.swing.JButton();
        JBaceptar = new javax.swing.JButton();

        jLabel2.setText("jLabel2");

        setClosable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Listado de Mensualidad Impagas"));

        jLabel1.setText("Periodo:");

        jTDatos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Rut", "Colegio", "Nombre", "Apellido", "Apoderado", "Domicilio"
            }
        ));
        jScrollPane2.setViewportView(jTDatos);

        jCBAnio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2009", "2010", "2011", "2012", "2013", "2014", "2015" }));

        jCBMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBcancelar.setText("Cancelar");
        jBcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcancelarActionPerformed(evt);
            }
        });

        JBaceptar.setText("Aceptar");
        JBaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBaceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(JBaceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBcancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBcancelar)
                    .addComponent(JBaceptar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JBaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBaceptarActionPerformed
        //Se hace la consulta de los pasajeros que no pagaron cierta mensualidad
        String mes = String.valueOf(jCBMes.getSelectedIndex() + 1); //el mes se obtiene del indice seleccionado del combobox con los meses
        if (mes.length() < 2) //si el indice es menor a 10, se le agrega un cero antes del numero
            mes = "0" + mes;
        String anno = jCBAnio.getSelectedItem().toString(); //el año se obtiene del combobox, convirtiendo a String su elemento seleccionado
        try {
            PreparedStatement pstm = JFPrincipal.con.getConnection().prepareStatement("select rut_pasajero, nombre_pasajero, apellido_pasajero " +
            "from pasajero " +
            "where rut_pasajero not in (select p.rut_pasajero " +
            "from mensualidad m, pasajero p " +
            "where year_mensualidad = '"+anno+"' and mes_mensualidad = '"+mes+"' and m.rut_pasajero = p.rut_pasajero);");                                   
            ResultSet res = pstm.executeQuery(); //se ejecuta la consulta
            int i = 0; //contador de registros devueltos
            while(res.next()){ //se recorren los registros para tener el total 
                i++;
            }
            System.out.println("numero de registros: " + i);
            res.first(); //se vuelve al primer registro
            Object[][] data = new String[i][3]; //se crea la matriz de datos para la tabla de informe
            for(int j = 0; j < i; j++) //se recorren los registros y se asignan a la matriz de datos
            {            
                String rut = res.getString("rut_pasajero");
                String nombre = res.getString("nombre_pasajero");
                String apellido = res.getString("apellido_pasajero");           
                data[j][0] = rut;            
                data[j][1] = nombre;            
                data[j][2] = apellido;            
                res.next();
            } 
            res.close(); 
            String[] columNames = {"rut","nombre","apellido"}; //encabezados de la columnas de la tabla
            //Se colocan los datos en una tabla de datos, sobreescribiendo el metodo isCellEditable
            //para que no se puedan modificar los datos manualmente
            DefaultTableModel datos = new DefaultTableModel(data,columNames){
                 @Override
                 public boolean isCellEditable(int row, int column) { return false; }
            };                      
            jTDatos.setModel(datos); //se asigna la tabla de datos al modelo de la tabla
            if (i == 0)
                JOptionPane.showMessageDialog(this, "No hay registros para el periodo seleccionado.","Advertencia", JOptionPane.WARNING_MESSAGE);
        }catch(SQLException e){
            System.out.println(e);
            JOptionPane.showMessageDialog(this, "No se ha podido generar el informe.","Error de Datos", JOptionPane.ERROR_MESSAGE);
        }    
    }//GEN-LAST:event_JBaceptarActionPerformed

    private void jBcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcancelarActionPerformed
         //El boton Cancelar cierra la ventana actual
        this.setVisible(false);
        this.dispose();
        JFPrincipal.nopago = null; //se asigna null a la variable que almacena la ventana actual
    }//GEN-LAST:event_jBcancelarActionPerformed

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        JFPrincipal.nopago = null; //se asigna null a la variable que almacena la ventana actual
    }//GEN-LAST:event_formInternalFrameClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBaceptar;
    private javax.swing.JButton jBcancelar;
    private javax.swing.JComboBox jCBAnio;
    private javax.swing.JComboBox jCBMes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTDatos;
    // End of variables declaration//GEN-END:variables
}
